﻿namespace BloodlustFury.DataMiner
{
	#region "Using Statements"

	using System;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Data;
	using System.Drawing;
	using System.Linq;
	using System.Text;
	using System.Windows.Forms;

	using Business;

	#endregion

	public partial class CaseLibraryForm : Form
	{
		public CaseLibraryForm()
		{
			InitializeComponent();

			if (DataEngine.CaseLibrary != null)
			{
				toolStripStatusLabel.Text = String.Format("Loaded: {0}", DataEngine.CaseLibrary.FileName);
				toolStripRecordCount.Text = String.Format("Record Count: {0}", DataEngine.CaseLibrary.Items.Count);

				CaseLibraryGridView.DataSource = DataEngine.CaseLibrary.Items;
			}
			else
			{
				toolStripStatusLabel.Text = "No file loaded.";
				toolStripRecordCount.Text = "Record Count: 0";
			}
		}

		private void LoadCaseLibraryButton_Click(object sender, EventArgs e)
		{
			DialogResult result = DialogResult.Yes;

			if (DataEngine.TestModulesLoaded && DataEngine.CaseLibraryLoaded)
			{
				result = MessageBox.Show("Loading a new file will delete any existing data you have already generated.\nDo you wish to continue?",
																 "Bloodlust Fury - DataMiner",
																 MessageBoxButtons.YesNo,
																 MessageBoxIcon.Question);
			}

			if (result == DialogResult.Yes)
			{
				if (OpenCaseLibraryFileDialog.ShowDialog() == DialogResult.OK)
				{
					try
					{
						DataEngine.LoadCaseLibrary(OpenCaseLibraryFileDialog.FileName);
						DataEngine.ClearPredictions();
						DataEngine.ClearStatistics();

						toolStripStatusLabel.Text = String.Format("Loaded: {0}", DataEngine.CaseLibrary.FileName);
						toolStripRecordCount.Text = String.Format("Record Count: {0}", DataEngine.CaseLibrary.Items.Count);

						CaseLibraryGridView.DataSource = DataEngine.CaseLibrary.Items;
					}
					catch (Exception ex)
					{
						MessageBox.Show(String.Format("Error loading case library:\n{0}", ex.Message),
														"Bloodlust Fury - DataMiner",
														MessageBoxButtons.OK,
														MessageBoxIcon.Error);
						return;
					}

					MessageBox.Show("Case Library has been loaded.", "Bloodlust Fury - DataMiner", MessageBoxButtons.OK, MessageBoxIcon.Information);
				}
			}

		}

		private void CaseLibraryForm_Load(object sender, EventArgs e)
		{
			DataEngine.OnCaseLibraryLoaded += this.OnCaseLibraryLoaded;
		}

		private void OnCaseLibraryLoaded(object sender, EventArgs e)
		{
			CaseLibraryGridView.DataSource = DataEngine.CaseLibrary.Items;
			CaseLibraryGridView.Refresh();

			toolStripStatusLabel.Text = String.Format("Loaded: {0}", DataEngine.CaseLibrary.FileName);
			toolStripRecordCount.Text = String.Format("Record Count: {0}", DataEngine.CaseLibrary.Items.Count);
		}
	}
}
